// pages/chooseTable/chooseTable.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadMore: true,
        noImage: "../../images/noImage.png",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this,
            item = JSON.parse(options.item);
        that.setData({
            isChinese: wx.getStorageSync("isChinese"),
            loadMore: true
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "选桌" : "Table Map"
        })
        console.log(item, item.tableOneCount, "ticketOneCount")
        let location = item.location
        location.count = 1
        location.balance = item.balance
        location.totalPrice = location.count * location.deposit
        location.tableOneCount = item.tableOneCount
        that.setData({
            eventId: item.eventId,
            id: options.id,
            eventType: item.eventType,
            venueName: app.globalData.venueName,
            eventName: app.globalData.eventName,
            cityName: app.globalData.cityName,
            eventLocation: app.globalData.location,
            location: location,
            tableOneCount: item.tableOneCount,
            items: item
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    jian(e) {
        let location = this.data.location;

        if (location.count <= 1) {
            return;
        }
        location.count--
            location.totalPrice = location.count * location.deposit
        this.setData({
            location: location
        })
    },
    jia(e) {
        let location = this.data.location;
        if (location.balance <= 0) {
            wx.showModal({
                title: this.data.isChinese ? "提示" : "Prompt",
                showCancel: false,
                content: this.data.isChinese ? '没有更多票啦!' : 'No more!'
            })
        }

        location.count++
            if (location.count > this.data.tableOneCount && this.data.tableOneCount > 0) {
                // wx.showModal({
                //     title: this.data.isChinese ? "提示" : "Prompt",
                //     showCancel: false,
                //     content: this.data.isChinese ? '单张限购' + ticket[index].ticketOneCount + '张' : 'Limit' + ticket[index].ticketOneCount + 'per order'
                // })
                return;
            }
        if (location.count > location.balance) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     showCancel: false,
            //     content: this.data.isChinese ? '没有更多票啦!' : 'No more!'
            // })
            location.count = location.balance
        }
        location.totalPrice = (location.count * location.deposit).toFixed(2)
        this.setData({
            location: location
        })
    },
    bindinput(e) {
        let value = e.detail.value;
        let location = this.data.location;
        this.max(value)
        location.count = value
        if (location.count > this.data.tableOneCount && this.data.tableOneCount > 0) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     showCancel: false,
            //     content: this.data.isChinese ? '单张限购' + ticket[index].ticketOneCount + '张' : 'Limit' + ticket[index].ticketOneCount + 'per order'
            // })
            location.count = this.data.tableOneCount
        }
        location.totalPrice = location.count * location.deposit
        this.setData({
            location: location
        })
    },
    max(value) {
        if (value > this.data.location.balance) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     showCancel: false,
            //     content: this.data.isChinese ? '剩余数量不足' : 'Insufficient balance'
            // })
            value = this.data.location.balance
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    pay() {
        if (this.data.location.balance <= 0) {
            wx.showModal({
                title: this.data.isChinese ? "提示" : "Prompt",
                showCancel: false,
                content: this.data.isChinese ? '没有更多票啦!' : 'No more!'
            })
            return;
        }
        if (app.globalData.mobilePhone == "" || app.globalData.mobilePhone == app.globalData.unionId) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     content: this.data.isChinese ? "该用户并未注册" : "This user didn't register",
            //     showCancel: false,
            //     success: function(res) {
            //         if (res.confirm) {
            wx.navigateTo({
                    url: '../login/login'
                })
                //         } else if (res.cancel) {
                //             console.log("用户点击取消");
                //         }
                //     }
                // });
            return;
        }

        var location = JSON.stringify(this.data.location)
        wx.navigateTo({
            url: '../pay/pay?eventId=' + this.data.eventId + "&id=" + this.data.id + "&location=" + location + "&isType=1",
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})